{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MongoDB Atlas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Provide URI to constructor, or use environment variable\n",
    "import pymongo\n",
    "from llama_index.vector_stores.mongodb import MongoDBAtlasVectorSearch\n",
    "from llama_index.indices.vector_store.base import VectorStoreIndex\n",
    "from llama_index.storage.storage_context import StorageContext\n",
    "from llama_index.readers.file.base import SimpleDirectoryReader"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# mongo_uri = os.environ[\"MONGO_URI\"]\n",
    "mongo_uri = \"mongodb+srv://<username>:<password>@<host>?retryWrites=true&w=majority\"\n",
    "mongodb_client = pymongo.MongoClient(mongo_uri)\n",
    "store = MongoDBAtlasVectorSearch(mongodb_client)\n",
    "storage_context = StorageContext.from_defaults(vector_store=store)\n",
    "uber_docs = SimpleDirectoryReader(input_files=[\"../data/10k/uber_2021.pdf\"]).load_data()\n",
    "index = VectorStoreIndex.from_documents(uber_docs, storage_context=storage_context)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "<b>\n",
       "Uber's revenue for 2021 was $17,455 million.</b>"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "response = index.as_query_engine().query(\"What was Uber's revenue?\")\n",
    "display(Markdown(f\"<b>{response}</b>\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4454\n",
      "1\n",
      "4453\n"
     ]
    }
   ],
   "source": [
    "from llama_index.response.schema import Response\n",
    "\n",
    "# Initial size\n",
    "\n",
    "print(store._collection.count_documents({}))\n",
    "# Get a ref_doc_id\n",
    "typed_response = response if isinstance(response, Response) else response.get_response()\n",
    "ref_doc_id = typed_response.source_nodes[0].node.ref_doc_id\n",
    "print(store._collection.count_documents({\"metadata.ref_doc_id\": ref_doc_id}))\n",
    "# Test store delete\n",
    "if ref_doc_id:\n",
    "    store.delete(ref_doc_id)\n",
    "    print(store._collection.count_documents({}))"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
